package com.icatw.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.icatw.blog.domain.Article;
import com.icatw.blog.domain.Category;
import com.icatw.blog.domain.User;
import com.icatw.blog.mapper.ArticleMapper;
import com.icatw.blog.service.IArticleService;
import com.icatw.blog.service.ICategoryService;
import com.icatw.blog.service.IUserService;
import com.icatw.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 文章Service业务层处理
 *
 * @author icatw
 * @date 2022-09-09
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private IUserService userService;

    @Autowired
    private ICategoryService categoryService;

    /**
     * 查询文章
     *
     * @param id 文章主键
     * @return 文章
     */
    @Override
    public Article selectArticleById(Long id) {
        return articleMapper.selectArticleById(id);
    }

    /**
     * 查询文章列表
     *
     * @param article 文章
     * @return 文章
     */
    @Override
    public List<Article> selectArticleList(Article article) {
        return articleMapper.selectArticleList(article);
    }

    /**
     * 新增文章
     *
     * @param article 文章
     * @return 结果
     */
    @Override
    public int insertArticle(Article article) {
        return articleMapper.insertArticle(article);
    }

    /**
     * 修改文章
     *
     * @param article 文章
     * @return 结果
     */
    @Override
    public int updateArticle(Article article) {
        return articleMapper.updateArticle(article);
    }

    /**
     * 批量删除文章
     *
     * @param ids 需要删除的文章主键
     * @return 结果
     */
    @Override
    public int deleteArticleByIds(Long[] ids) {
        return articleMapper.deleteArticleByIds(ids);
    }

    /**
     * 删除文章信息
     *
     * @param id 文章主键
     * @return 结果
     */
    @Override
    public int deleteArticleById(Long id) {
        return articleMapper.deleteArticleById(id);
    }

    @Override
    public int updateStatus(Long id) {
        return articleMapper.updateStatusById(id);
    }

    @Override
    public List<Article> selectArticleListVo(Article article) {
        QueryWrapper<Article> wrapper = new QueryWrapper<>();

        if (StringUtils.isNotNull(article)) {
            if (StringUtils.isNotBlank(article.getTitle())) {
                wrapper.like("title", article.getTitle());
            }
            if (article.getCategoryId()!=null) {
                wrapper.eq("category_id", article.getCategoryId());
            }
        }

        List<Article> articleList = this.list(wrapper);
        return articleList.stream().map(art -> {
            Long authorId = art.getAuthorId();
            Long categoryId = art.getCategoryId();
            Category category = categoryService.selectCategoryById(categoryId);
            User user = userService.selectUserById(authorId);
            art.setUser(user);
            art.setCategory(category);
            return art;
        }).collect(Collectors.toList());
    }
}
